package com.zrz.util;


import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 *
 * @author
 * @since 2015/12/30 13:23
 */
public final class DateUtils {
    /**
     * 毫秒
     */
    public static final int MILE_SECOND = 1000;
    /**
     * 一年的秒数
     */
    public static final int ONE_YEAR_SECOND = 365 * 24 * 3600;
    /**
     * 一分钟的秒数
     */
    public static final int ONE_MIN_SECOND = 60;
    /**
     * 一小时的秒数
     */
    public static final int ONE_HOUR_SECOND = 60 * 60;
    /**
     * 一天的秒数
     */
    public static final int ONE_DAY_SECOND = 24 * 60 * 60;
    /**
     * 一天的耗秒数
     */
    public static final int ONE_DAY_MILL_SECOND = 24 * 60 * 60 * 1000;


    /**
     * 获取当前时间
     *
     * @return 当前时间对应的秒数
     * @author
     * @since 2016/9/6 22:43
     */
    public static Long getCurrentMillsTime() {
        return System.currentTimeMillis();
    }


    public static String format(Long value, DateTimeFormatter dateTimeFormatter) {
        if (value == null || value == 0) {
            return StringUtils.EMPTY;
        }
        try {
            return new DateTime(value).toString(dateTimeFormatter);
        } catch (Exception ex) {
            return StringUtils.EMPTY;
        }
    }


    public static long parse(String date, String formatStr) {
        try {
            DateTimeFormatter format = DateTimeFormat.forPattern(formatStr);
            //时间解析
            DateTime dateTime = DateTime.parse(date, format);
            return dateTime.getMillis();
        } catch (Exception ex) {
            return 0;
        }
    }


    /**
     * 当天时间的00:00:00 （秒）
     *
     * @return long
     * @since 2017-9-6 16:29:46
     */
    public static long currentDayStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return calendar.getTime().getTime();
    }

    /**
     * 当天时间的23:59:59（秒）
     *
     * @return long
     * @since 2017-9-6 16:29:46
     */
    public static long currentDayEnd() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return calendar.getTime().getTime();
    }


    /**
     * 获取昨天的日期，精确到天
     *
     * @return 今天的日期
     */
    public static long getYestoday() {
        Date d = new Date(new Date().getTime() - ONE_DAY_MILL_SECOND);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return Long.parseLong(sdf.format(d));
    }

    public static Date addYear(Integer year) {

        Calendar calendar = new GregorianCalendar();
        Date date = new Date();
        calendar.setTime(date);
        calendar.add(calendar.YEAR, year);//把日期往后增加一年.整数往后推,负数往前移动
        date = calendar.getTime();
        return date;
    }

    public static String getDateBySimplDate(Date date, String str) {
        SimpleDateFormat format = new SimpleDateFormat(str);
        return format.format(date);
    }

    public static Date getDaySub() {
        Date date = new Date();//获取当前时间    
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);//当前时间前去一个月，即一个月前的时间
        return calendar.getTime();
    }

}